//
//  SurpriseShopDetailOneStyleCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/14.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class SurpriseShopDetailOneStyleCell: BaseTableViewCell {
    
    @IBOutlet weak var qiHaoLabel: UILabel!
    @IBOutlet weak var ingLabel: UILabel!
    @IBOutlet weak var bottomLineView: UIView!
    
    @IBOutlet weak var leftLabel: UILabel!
    
    @IBOutlet weak var middleLabel: UILabel!
    
    @IBOutlet weak var rightLabel: UILabel!
    
    @IBOutlet weak var progressBottomView: UIView!
    
    @IBOutlet weak var progressViewH: NSLayoutConstraint!
    @IBOutlet weak var progressTopView: UIView!
    
    var model : SurpriseShopDetModel?{
        
        didSet{
            if model == nil {
                return
            }
            
            qiHaoLabel.text = "期号: \(model!.od_no)"
            
            
            leftLabel.attributedText = attributedString(str: "已参与\(model!.od_buy_count)人次", font: UIFont.systemFont(ofSize: 14))
            
            middleLabel.attributedText = attributedString(str: "总计\(model!.od_count)人次", font: UIFont.systemFont(ofSize: 14))
            
            rightLabel.attributedText = attributedString(str: "剩余\(model!.od_remain_count)人次", font: UIFont.systemFont(ofSize: 14))
            
            progressViewH.constant = 0
            
            let temp = model!.od_progress.replacingOccurrences(of: "%", with: "")
            
            
            if let prosess = Float(temp) {
                
                progressViewH.constant = (KScreenW - 20) / 100 * CGFloat(prosess)
                
            }
            

        }
        
        
    }
    
    
    
    func attributedString(str: String, font: UIFont) -> NSAttributedString? {
        
        //编写规则
        let patten = "[0-9]{1,15}"
        //创建正则表达式对象
        
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: patten, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res = regex.firstMatch(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range:  NSRange(location: 0, length: str.characters.count))
        
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        strM.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(0, str.characters.count))
        
        strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.dakGray(), range: NSMakeRange(0, str.characters.count))
        
        
        
        
        if let temp = res?.range {
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: temp)
            
        }
        
        return strM
        
        
        
        
    }


    override func awakeFromNib() {
        super.awakeFromNib()
        contentView.backgroundColor = UIColor.white
        
        ingLabel.addCornerAndBorder(borderColor: nil, borderWidth: 0, masksToBounds: true, cornerRadius: 5)
        
        progressBottomView.backgroundColor = UIColor.lineGray()
        
        progressTopView.backgroundColor = UIColor.orange
        
        progressBottomView.addCornerAndBorder(borderColor: nil, borderWidth: 0, masksToBounds: true, cornerRadius: 6)
        
        
        bottomLineView.backgroundColor = UIColor.viewBgGrey()
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
